package com.example.mediamoudle.main.adapter

import android.util.Log
import com.android.baselibrary.base.BaseAdapter
import com.android.baselibrary.utils.dp2px
import com.android.baselibrary.utils.getScreenWidth
import com.example.mediamoudle.main.bean.MediaBean
import com.example.mediamoudle.R
import com.example.mediamoudle.databinding.MediaItemBinding

class MediaAdapter:BaseAdapter<MediaBean,MediaItemBinding>() {
    override fun getItemLayoutId(): Int = R.layout.media_item
    private var mClick:((MediaBean)->Unit)?=null

    override fun covert(binding: MediaItemBinding, t: MediaBean, position: Int) {
        binding.tvMedia.text = t.name
        binding.tvMedia.setOnClickListener {
            Log.i("TAG", "click: root")
            mClick?.invoke(t)
        }
        val dividerWidth = dp2px(binding.root.context,8)*3
        val itemWidth = (getScreenWidth() - dividerWidth) /2
        val layoutParams = binding.tvMedia.layoutParams
        var itemHeight = (itemWidth-dp2px((100*Math.random()).toInt()) * (9 / 16.0f))// 16：9
        layoutParams.width = itemWidth
        layoutParams.height = itemHeight.toInt()
        binding.tvMedia.layoutParams = layoutParams




    }
    fun setonClickItemListener(click:(MediaBean)->Unit){
        this.mClick = click
    }
}